package org.jeecg.crawler.content;


import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.DateTimeUtil;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.crawlerpaper.entity.CrawlerInfo;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.jeecg.modules.crawlerpaper.service.ICrawlerInfoService;
import org.jeecg.modules.system.service.ISysDictService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Slf4j
public class ConetentCrawlerJob  implements Job {
    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    @Autowired
    private ICrawlerInfoService crawlerInfoService;

    @Autowired
    private ISysDictService sysDictService;
    //http://njrb.njdaily.cn/html /2020-11/26 /node_51.htm
    //http://njrb.njdaily.cn/html,0,/node_1.htm,0 ,conetntCss ,detailCss
    //参数：baseurl,dataurl,page,scoureName,articleTime;

    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        if(parameter !=null){
            String[] params = parameter.split(",");
            if(params[1].equals("0")){
                params[1] = "/"+ DateFormatUtils.format(new Date(), "yyyy-MM/dd");

            }
            if(params[4].equals("0")){
                params[4] = DateTimeUtil.getTodayChar8En();
            }
            this.getCrewlerInfo(params[0],params[1],params[2],params[3],params[4],params[5],params[6]);

        }
        log.info(String.format(" Jeecg-Boot 普通定时任务 SampleJob !  时间:" + DateUtils.getTimestamp()));
    }


    public void  getCrewlerInfo(String baseUrl, String dateUrl,String page,String sourceName,String articleTime,String contentCss,String detailCss) throws Exception {
        ContentNewsCrawler crawler = new ContentNewsCrawler("crawl", true,baseUrl+dateUrl,page,contentCss);
        crawler.start(1);
        Map<String, String> urlMap = crawler.getUrlMap();

        DetailContentNewsCrawler crawlerDetail = new DetailContentNewsCrawler("crawl", true,urlMap,baseUrl+dateUrl,sourceName,articleTime,detailCss);
        crawlerDetail.start(1);
        List<CrawlerInfo> crawlerInfoList = crawlerDetail.getCrawlerInfoList();
        if(!CollectionUtils.isEmpty(crawlerInfoList)){
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){

                Iterator<CrawlerInfo> iterator = crawlerInfoList.iterator();
                while (iterator.hasNext()){
                    CrawlerInfo next = iterator.next();
                    Boolean removeFlag = true;
                    for (DictModel keyword : keywords) {
                        if(next.getArticleName().contains(keyword.getValue())||next.getArticleContent().toString().contains(keyword.getValue())){
                            removeFlag = false;
                            break;
                        }
                    }
                    if(removeFlag){
                        iterator.remove();
                    }

                }
            }
            if(!CollectionUtils.isEmpty(crawlerInfoList)){
                crawlerInfoService.saveOrUpdateBatch(crawlerInfoList);

            }

        }
    }
}
